package com.aim.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.aim.security.JettyPasswordEncoder;
import com.aim.security.UsersDetailsService;

/**
 * @AUTO 权限配置类
 * @FILE SecurityConfig.java
 * @DATE 2021年4月25日 上午9:43:33
 * @Author AIM
 * @Version
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	/**
	 * 定义用户信息
	 * 
	 * @return
	 */
	@Bean
	public UserDetailsService userDetailsService() {
		return new UsersDetailsService();
	}

	/**
	 * 密码编码器
	 * 
	 * @return
	 */
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new JettyPasswordEncoder();
	}

	// 配置安全拦截机制
	protected void configure(HttpSecurity http) throws Exception {

		// 访问 /r/**资源需要登录认证，其他资源放开，允许表单登录，登录成功跳转/login-success
		http.authorizeRequests().antMatchers("/r/r1").hasAnyAuthority("p1").antMatchers("/r/r2").hasAnyAuthority("p2")
				.antMatchers("/r/**").authenticated().anyRequest().permitAll().and().formLogin()
				.successForwardUrl("/login-success");
	}

}
